package com.baitiaojun.bulletscreen.config;

import com.baitiaojun.bulletscreen.config.properties.MqSyncMysqlProperties;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @类名 RabbitmqConfig
 * @描述 TODO
 * @作者 白条君
 * @创建日期 2022/11/26 20:05
 * @版本 1.0
 */
@Configuration
public class RabbitmqSyncMysqlConfig {

    @Resource
    private MqSyncMysqlProperties properties;

    @Bean
    public Exchange exchange () {
        return ExchangeBuilder.topicExchange(properties.getSyncMysqlExchange())
                .durable(true)
                .alternate(properties.getBackupSyncMysqlExchange())
                .build();
    }

    @Bean
    public Queue queue() {
        return QueueBuilder.durable(properties.getSyncMysqlQueue())
                .deadLetterExchange(properties.getDeadSyncMysqlExchange())
                .deadLetterRoutingKey(properties.getDeadSyncMysqlRoutingKey())
                .maxLength(1000)
                .ttl(5000)
                .build();
    }

    @Bean
    public Binding queueBindingExchange(@Qualifier("queue") Queue queue
            , Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(properties.getSyncMysqlRoutingKey()).noargs();
    }

    @Bean
    public Exchange deadExchange() {
        return ExchangeBuilder.topicExchange(properties.getDeadSyncMysqlExchange())
                .durable(true)
                .build();
    }

    @Bean
    public Queue deadQueue() {
        return QueueBuilder.durable(properties.getDeadSyncMysqlQueue()).build();
    }

    @Bean
    public Binding deadQueueBindingDeadExchange(@Qualifier("deadQueue") Queue deadQueue
            , @Qualifier("deadExchange") Exchange deadExchange) {
        return BindingBuilder.bind(deadQueue).to(deadExchange).with(properties.getDeadSyncMysqlRoutingKey()).noargs();
    }

    @Bean
    public Exchange backupExchange() {
        return ExchangeBuilder.fanoutExchange(properties.getBackupSyncMysqlExchange()).durable(true).build();
    }

    @Bean
    public Queue backupQueue() {
        return QueueBuilder.durable(properties.getBackSyncMysqlQueue()).build();
    }

    @Bean
    public Queue warningQueue() {
        return QueueBuilder.durable(properties.getWarningSyncMysqlQueue()).build();
    }

    @Bean
    public Binding backQueueBindingBackExchange(@Qualifier("backupQueue") Queue backupQueue
            , @Qualifier("backupExchange") Exchange backupExchange) {
        return BindingBuilder.bind(backupQueue).to(backupExchange).with("").noargs();
    }

    @Bean
    public Binding warningQueueBindingBackExchange(@Qualifier("warningQueue") Queue warningQueue
            , @Qualifier("backupExchange") Exchange backupExchange) {
        return BindingBuilder.bind(warningQueue).to(backupExchange).with("").noargs();
    }
}